package com.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

public class CircleConsoleView extends View {

    private Paint mPaintForCircle;
    private OnCircleConsoleViewTouchListener mOnCircleConsoleViewTouchListener;
    // 梯度渲染
    Shader mSweepGradient = null;
    private final float DEGREE_UNIT = 45.0f;
    public  final static int  AVIOCTRL_PTZ_LEFT = 1;
    public final static int  AVIOCTRL_PTZ_RIGHT = 2;
    public final static int  AVIOCTRL_PTZ_TOP = 3;
    public final static int  AVIOCTRL_PTZ_BOTTOM = 4;
    public CircleConsoleView(Context context) {
        super(context);
    }

    public CircleConsoleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CircleConsoleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    /**
     * Compare to: {@link android.view.View#getDefaultSize(int, int)}
     * If mode is AT_MOST, return the child size instead of the parent size
     * (unless it is too big).
     */
    private static int getDefaultSize2(int size, int measureSpec) {
        int result = size;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        switch (specMode) {
            case MeasureSpec.UNSPECIFIED:
                result = size;
                break;
            case MeasureSpec.AT_MOST:
                result = Math.min(size, specSize);
                break;
            case MeasureSpec.EXACTLY:
                result = specSize;
                break;
        }
        return result;
    }

    public void setHandleReaction(OnCircleConsoleViewTouchListener onCircleConsoleViewTouchListener) {
        mOnCircleConsoleViewTouchListener = onCircleConsoleViewTouchListener;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize2(getSuggestedMinimumWidth(), widthMeasureSpec),
                getDefaultSize2(getSuggestedMinimumHeight(), heightMeasureSpec));
        int childWidthSize = getMeasuredWidth();
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawColor(Color.TRANSPARENT);

        int radiusOuter = getWidth() / 2;
        int radiusInner = getWidth() / 5;
        float cx = getWidth() / 2;
        float cy = getHeight() / 2;

        if (null == mPaintForCircle) {
            mPaintForCircle = new Paint();
        }

        mPaintForCircle.setAntiAlias(true);
        mPaintForCircle.setStyle(Paint.Style.FILL);
        mSweepGradient = new LinearGradient(0, 0, getWidth(),  getWidth(), Color.TRANSPARENT, Color.TRANSPARENT, Shader.TileMode.REPEAT);
        if (null == mOnCircleConsoleViewTouchListener || null == mOnCircleConsoleViewTouchListener.getTouchPosition()) {
            mPaintForCircle.setColor(Color.TRANSPARENT);
            mPaintForCircle.setShader(mSweepGradient);
            canvas.drawCircle(cx, cy, radiusInner, mPaintForCircle);
            canvas.save();
            return;
        }
        float[] touchPosition = mOnCircleConsoleViewTouchListener.getTouchPosition();
        double ratio = (radiusOuter - radiusInner) / Math.sqrt(
                        Math.pow(touchPosition[0] - cx , 2) + Math.pow(touchPosition[1] - cy ,
                                2));

        float currentX =0;
        float currentY = 0;
        if (ratio>=1.0){
            currentX = touchPosition[0];
            currentY = touchPosition[1];
        }else {
            currentX = (float) (ratio * (touchPosition[0] - cx ) + cx);
            currentY = (float) (float) (ratio * (touchPosition[1] - cy ) + cy);
        }

        float a = touchPosition[0]-radiusOuter;// a边为对边 cx2-radiusOuter
        float b = touchPosition[1] - radiusOuter;// b边为邻边 cy2-radiusOuter

        double B = Math.atan(a / b);
        // 弧度转换成角度
        B = Math.toDegrees(B);
        // 以x轴为分界线，以180°计算，算出的角度如果为负数，+180
        B = B < 0 ? B + 180 : B;
        int event  =0 ;
        int startAngle = 0;
        if (currentX<=radiusOuter){
            if (B <= DEGREE_UNIT){
                event = AVIOCTRL_PTZ_TOP;
            }else if (B<=DEGREE_UNIT*3){
                event = AVIOCTRL_PTZ_LEFT;
            }else if (B>DEGREE_UNIT*3){
                event = AVIOCTRL_PTZ_BOTTOM;
            }
        }else{

            if (B <= DEGREE_UNIT){
                event = AVIOCTRL_PTZ_BOTTOM;
            }else if (B<=DEGREE_UNIT*3){
                event = AVIOCTRL_PTZ_RIGHT;
            }else if (B>DEGREE_UNIT*3){
                event = AVIOCTRL_PTZ_TOP;
            }

        }
        if (event == AVIOCTRL_PTZ_TOP){
            startAngle = 225;
        } else if (event == AVIOCTRL_PTZ_RIGHT){
            startAngle = 315;
        }else if (event == AVIOCTRL_PTZ_BOTTOM){
            startAngle = 45;
        }else if (event == AVIOCTRL_PTZ_LEFT){
            startAngle = 135;
        }
        Log.d("HandleView", String.format("cx2：%f  cy2:%f B: %f event:%d", a, b,B ,event));
        Shader mShader = new RadialGradient(radiusOuter, radiusOuter, radiusOuter, new int[] {Color.TRANSPARENT,
                Color.TRANSPARENT},new float[] {0.4f,1.0f}, Shader.TileMode.REPEAT);
        mPaintForCircle.setShader(mShader);
        RectF rectF = new RectF(0, 0,getWidth(),getWidth()); // 设置矩形区域
        canvas.drawArc(rectF, startAngle, 90, true, mPaintForCircle);
        canvas.save();
        mPaintForCircle.setShader(mSweepGradient);
        mPaintForCircle.setColor(Color.TRANSPARENT);
        canvas.drawCircle(currentX, currentY, radiusInner, mPaintForCircle);
        canvas.save();
        if (mOnCircleConsoleViewTouchListener!=null){
            mOnCircleConsoleViewTouchListener.onDirectionChanged(event);
        }
    }


    public interface OnCircleConsoleViewTouchListener {

        float[] getTouchPosition();
        void   onDirectionChanged(int direction);
    }
}
